package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    Teacher Teacher = new Teacher();

    Teacher[] T = new Teacher[5];

    //添加老师的方法，含boolean返回值，true代表添加成功
    public boolean addTeacher(Teacher t) {
        //1.创建Teacher数组，长度为5，为了提高数组作用域，移至成员变量位置。
        //2.将接受的老师对象添加进数组
        int index = -1;
        for (int i = 0; i < T.length; i++) {
            Teacher temp = T[i];
            if (temp == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            T[index] = t;
            return true;
        }
        //3.返回是否添加成功

    }

    //返回现有数组的办法
    public Teacher[] findAllStudent() {
        return T;
    }

    //查找id对应索引，并用null替换该索引上的对象的删除方法
    public void deleteTeacherById(String id) {

        //调用查找老师id对应的索引的方法，返回索引
        int index = getindex(id);
        //将索引上的对象用null替换
        T[index] = null;
    }

    //根据老师id查找对应索引的方法
    public int getindex(String id) {
        //查找老师id对应的索引
        int index = -1;
        for (int i = 0; i < T.length; i++) {
            Teacher Temp = T[i];
            if (Temp != null && Temp.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(String id, com.itheima.edu.info.manager.domain.Teacher newTeacher) {
        int index = getindex(id);
        T[index] = newTeacher;
    }
}
